﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SHDQGSWEB.Models
{
    public class GadgetRepository
    {
        SHDQGSDBDataContext db = new SHDQGSDBDataContext();
        public bool Save()
        {
            try
            {
                db.SubmitChanges();
            }
            catch (Exception)
            {

                return false;
            }
            return true;
        }
    
        public IQueryable<Gadget> FindGadgets(string gadgetName)
        {
            return from gadget in db.Gadget
                   where gadget.Name == gadgetName
                   select gadget;
        }
        /// <summary>
        /// find gedget by id
        /// </summary>
        /// <param name="gadgetId"></param>
        /// <returns></returns>
        public Gadget FindGadget(int gadgetId)
        {
            return db.Gadget.Single(gadget => gadget.GadgetID == gadgetId);

        }
        
        public bool AddGadget(Gadget gadget)
        {
            try
            {
                db.Gadget.InsertOnSubmit(gadget);
                
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
        
        public bool DeleteGadget(Gadget gadget)
        {
            try
            {
                db.Gadget.DeleteOnSubmit(gadget);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
    }
}